package com.vlin.scene.actuator;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.vlin.entity.scene.Automation;
import com.vlin.entity.scene.Scene;
import com.vlin.entity.scene.TimerEvent;

public abstract class AbstractSceneActuator implements SceneActuator {

	ExecutorService executorService = new ThreadPoolExecutor(2, 2, 
            0, TimeUnit.SECONDS, 
            new ArrayBlockingQueue<>(512), 
            new ThreadPoolExecutor.CallerRunsPolicy());
	
	@Override
	public void execute(Scene scene) {

		executorService.submit(new Runnable() {
			
			@Override
			public void run() {
				TimerEvent timerEvent;
				if( getSceneResolver() != null 
						&& (timerEvent = getSceneResolver().resolve(scene)) != null 
						&& getTimerExecutor() != null) {
					getTimerExecutor().execute(timerEvent);
				}
				
			}
		});
		
	}

	@Override
	public void execute(Automation automation) {
		executorService.submit(new Runnable() {
			
			@Override
			public void run() {
				TimerEvent timerEvent;
				if( getSceneResolver() != null 
						&& (timerEvent = getSceneResolver().resolve(automation)) != null 
						&& getTimerExecutor() != null) {
					getTimerExecutor().execute(timerEvent);
				}
				
			}
		});
		
	}

}
